/**
 * Created by Xiaoming on 2017/9/2.
 */
angular.module('module.order')
    .controller('OrderHistoryController', OrderHistoryController);
OrderHistoryController.$inject = ['$scope', '$state','$cookies', '$stateParams', 'OrderService', 'ShopMenuService', 'AuthUserService'];
function OrderHistoryController($scope, $state,$cookies, $stateParams, OrderService, ShopMenuService, AuthUserService) {
    var vm = this;
    $cookies.put("tag","message");
    vm.goback = function () {
        if($stateParams.succese == 1){
            $state.go("store",{shopid:$cookies.get("shopid"),deskid:$cookies.get("deskid")})
        }else {
            history.go(-1);
        }
    };
    vm.user = AuthUserService.getUserByCookies();
    var shopid = $stateParams.shopid;
    var deskid = $stateParams.deskid;
    var appid = $stateParams.appid;
    if (vm.user.userId == null) {
        $('.nologinshow').show();
    } else {
        vm.allorders = [];
        //全部订单
        vm.page = {
            num: 10,
            n: 1,
            //paystate: 1,
            type: 6
        };
        vm.no_order_text = "正在获取订单......";
        var timestamp = '', limit = 10 * 60000, remainingTime = 0;
        //所有订单
        vm.all = function () {
            OrderService.getOrderByUid(vm.page).$promise.then(function (orderList) {
                timestamp = Date.parse(new Date());
                for (var i = 0; i < orderList.length; i++) {
                    if (orderList[i].type != 4 && orderList[i].paystate != 1) {
                        if(orderList[i].status == 0){
                            remainingTime = limit - (timestamp - orderList[i].createTime);
                            orderList[i].remainingTime = remainingTime;
                            setRemainingTime(orderList[i]);
                        }
                    }
                    vm.allorders.push(orderList[i]);
                }
                var order_length = vm.page.n * vm.page.num;
                if (order_length >= vm.allorders.length) {
                    vm.load0 = false;
                }
                else {
                    vm.load0 = true;
                }
                if (vm.allorders.length != 0) {
                    vm.flat0 = true;
                } else {
                    vm.no_order_text = "没有相关订单！";
                    vm.flat0 = false;
                }
            })
        };
        vm.all();
        $(window).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight >= scrollHeight - 50) {
                if (!vm.load0 && vm.flat0) {
                    vm.load0 = true;
                    vm.page.n++;
                    vm.all();
                }
            }
        })
    }
    vm.nowlogin = function () {
        $state.go('login');
    };
    //点击进入当前商店
    vm.enterShop = function (shopid, scope) {
        $cookies.put("tag","store");
        $state.go("store", {shopid: shopid,appid:appid,deskid:deskid, scope: scope});
    };
    //继续支付
    vm.immpay = function (order) {
        $state.go("morepay",{orderid:order.orderid,shopid:$stateParams.shopid,appid:appid,deskid:$stateParams.deskid});
    };
    //

    //判断订单是否超过支付时间
    function setRemainingTime(order) {
        if (order.remainingTime > 0) {
            setTimeout(function () {
                timestamp = Date.parse(new Date());
                remainingTime = limit - (timestamp - order.createTime);
                order.remainingTime = remainingTime;
                setRemainingTime(order);
                $scope.$digest();
            }, 1000)
        } else {
            order.status = 4;
            OrderService.updateOrderById(order, function (data) {
            }, function () {

            })
        }
    };
    //再来一单功能
    vm.anotherOne = function (shop) {
        $cookies.put('tag', 'store');
        $state.go("store", {shopid:shopid,appid:appid,deskid:deskid,scope:true});
        //以下考虑到下架商品问题暂时注释
        /*ShopMenuService.getMenuByShopid(shop.shopid).$promise.then(function (data) {
         for (var i = 0; i < shop.goodsDetailList.length; i++) {
         var good = shop.goodsDetailList[i];
         ShopMenuService.anotherOneOrder(shop.shopid, good.goodsid, good.num, i);
         }
         var shopList = ShopMenuService.buyNowShopById(shop.shopid);
         setCookie('tag', 'store');
         //$state.go("firmorder", {order: shopList});
         $state.go("store", {shopid:shopid,deskid:deskid,scope:true});
         });*/
    };

    //重新下单
    vm.neworder = function (order) {
        OrderService.cancelorderbyid(order.orderid,function(data){
            var shop = order.ordersShops[0];
            ShopMenuService.getMenuByShopid(shop.shopid).$promise.then(function (data) {
                for (var i = 0; i < shop.goodsDetailList.length; i++) {
                    var good = shop.goodsDetailList[i];
                    ShopMenuService.addGoodByOrder(shop.shopid, good.goodsid, good.num, i,good.skuid);
                }
                //var shopList = ShopMenuService.buyNowShopById(shop.shopid);
                $state.go("store", {shopid:order.ordersShops[0].shopid,appid:appid,deskid:order.ordersShops[0].osid});
                //setCookie('placeorder', 'order');
                //$state.go("firmorder", {order: shopList});
            });
        },function(){

        });
    };


    //返回按钮
    vm.goback = function () {
        window.history.back();
        //$state.go("store",{shopid:$stateParams.shopid,deskid:$stateParams.deskid});
    };


    //跳转订单具体信息
    vm.orderdetail = function (order) {
        $state.go("orderdetail",{orderid:order.orderid,shopid:order.ordersShops[0].shopid,appid:appid,deskid:$stateParams.deskid});
    }
}